<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 订单酒店信息表模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Orderhotel_model extends CI_Model
{
    private $hotel_table ;
    private $info_table ;
    private $contacts_table ;


    public function __construct()
    {
        parent::__construct();
        $this->info_table = $this->db->dbprefix("order_info");
        $this->contacts_table = $this->db->dbprefix("order_contacts");
        $this->order_hotel_table = $this->db->dbprefix("order_hotel");

        $this->order_room_table = $this->db->dbprefix("order_room");
    }


    /**
     * 用户中心获取酒店订单列表
     * @param int $page_size 每页显示数目
     * @param int $now_page 第几页
     * @param mixed $where 查询条件
     * @return array
     */
    public function get_order_list($page_size = 20, $now_page = 1, $where='',$fields = '*')
    {

        $this->db->select($fields);
        $this->db->join($this->order_hotel_table, $this->info_table . ".order_id=$this->order_hotel_table.order_id", 'left');
        $this->db->join($this->contacts_table, $this->info_table . ".order_id=$this->contacts_table.order_id", 'left');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($this->info_table . '.add_time', 'DESC');
        $query = $this->db->get($this->info_table, $page_size, ($now_page - 1) * $page_size);
        return $query->result_array();
    }

    /**
     * 获取酒店订单总数量
     * @param string $where 查询条件
     * @return mixed
     */
    public function get_order_count($where = '')
    {
        $table = $this->db->dbprefix("order_info");
        $contact_table = $this->db->dbprefix("order_contacts");
        $this->db->select($table . '.order_id,' . $table . '.order_sn,' . $table . '.order_account,' . $table . '.paid_account,' . $table . '.order_status,' . $table . '.pay_status,' . $table . '.add_time,' . $table . '.user_id');
        $this->db->select($contact_table . '.name as contact_name,' . $contact_table . '.email as contact_email,' . $contact_table . '.phone_num as phone_num');
        $this->db->join($contact_table, $contact_table . ".order_id=$table.order_id", 'inner');
        $this->db->join($this->order_hotel_table, $this->info_table . ".order_id=$this->order_hotel_table.order_id", 'left');

        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->select('order_id');
        $this->db->from($table);
        return $this->db->count_all_results();
    }

    /**
     * 更新数据
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data,$where){
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($this->order_hotel_table,$data);
    }



    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data){
         if(is_array($data)){
            if($this->db->insert($this->order_hotel_table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where,$field='*')
    {
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->order_hotel_table,1);
        return $query->row_array();
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function remove_row($where)
    {
        if(!empty($where))return $this->db->delete($this->order_hotel_table,$where);
        return false;
    }


    /**
     * 获取订单状态 ，支付状态
     */
    public function _set_order_status_name()
    {
        $pay_status = array('', '未支付', '部分支付', '支付完成');
        $order_status = array('', '未审核', '已审核', '已取消', '已完成');
        $cat_type = array('', '门市订单', '线上订单', '掌旅宝订单', '团购订单', '', '手机订单', '', '分销商订单','', '同业订单');
        $data = array('order_status' => $order_status, 'pay_status' => $pay_status, 'cat_type' => $cat_type);
        return $data;
    }


 

}